/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

class mysqlBridge {

    public Connection conn;
    public Statement stm;
    public String host;
    public String db_name;
    public String username;
    public String password;

    public mysqlBridge() {
        this.setParameters("localhost", "java_mysql", "root", "");

        try {
            Class c = Class.forName("com.mysql.jdbc.Driver");
            String db_url = "jdbc:mysql://" + this.host + ":3306/" + this.db_name;
            this.conn = DriverManager.getConnection(db_url, this.username, this.password);

            this.stm = this.conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void setParameters(String db_host, String db_name, String db_user, String db_pass) {
        this.host = db_host;
        this.db_name = db_name;
        this.username = db_user;
        this.password = db_pass;
    }

    public void closeConnection() {
        try {
            this.conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ResultSet getBlogRecord(String blogID) {
        try {
            String query = "SELECT * FROM `blog_list` WHERE" +
                    " `blogID`='" + blogID + "'";

            ResultSet resl = this.stm.executeQuery(query);
            return resl;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param table_name
     * @param col1
     * @param col1_val
     * @param col2
     * @param col2_val
     */
    public void addToDB(String table_name, String col1, String col1_val, String col2, String col2_val) {
        try {
            ResultSet rel = this.getBlogRecord(col1_val);
            if (!rel.next()) {
                String query = "INSERT INTO `" + table_name + "` (`" + col1 + "`,`" + col2 + "`) VALUES" +
                        " ('" + col1_val + "', '" + col2_val + "')";

                this.stm.executeUpdate(query);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

public void updateDB(String table_name, String field, String value, String cond, String condVal) {
        String query = "UPDATE `" + table_name + "` SET `" + field + "`='" + value + "' WHERE `" + cond + "`='" + condVal + "'";
        //System.out.println(query);
        try {
            this.stm.executeUpdate(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

	public void customUpdate(String query) {
        try {
            this.stm.executeUpdate(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

	public ResultSet customQuery(String query) {
        try {
            ResultSet rsl = this.stm.executeQuery(query);
            return rsl;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    

}

public class getToDB {


    public static void main(String args[]) {
        mysqlBridge imple = new mysqlBridge();
//        int DMax=0;
//        if(args.length <= 1)  System.out.println("ERROR XYZ");
//        else {
//            try {
//                String blogID = args[0];
//                int newDMax = Integer.parseInt(args[1]);
//                ResultSet re = imple.customQuery("select * from blog_list where id=1");
//                while (re.next()) {
//                    DMax = re.getInt("DMax");
//                }
//                if (newDMax > DMax) {
//                    String update = "update blog_list set blogID=\"" + blogID + "\", DMax=" + newDMax + "where id=1";
//                    imple.customUpdate(update);
//                    System.out.println(DMax);
//                }
//            } catch (SQLException ex) {
//                Logger.getLogger(getToDB.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
        imple.addToDB("blog_list", "blogID", args[0], "friends", "");
    }
}
